﻿using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TaskManagement.Utils
{
    public static class QuartzHelpers
    {
        ///保证一个实例
        private static IScheduler _scheduler;

        /// <summary>
        /// 时间间隔
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ts"></param>
        /// <param name="jobName"></param>
        /// <param name="triggerName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public static async Task<IScheduler> StartAsync<T>(TimeSpan ts, string jobName, string triggerName, string groupName,string content) where T : IJob
        {
            DateTimeOffset runTime = DateBuilder.EvenSecondDate(DateTime.Now);
            IJobDetail job = JobBuilder.Create<T>().WithIdentity(jobName, groupName).UsingJobData("content", content).Build();
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(triggerName, groupName).StartAt(runTime).WithSimpleSchedule(x => x.WithInterval(ts).RepeatForever()).Build();
            if (_scheduler == null)
            {
                ISchedulerFactory factory = new StdSchedulerFactory();
                _scheduler = await factory.GetScheduler();
            }
            await _scheduler.ScheduleJob(job, trigger);
            await _scheduler.Start();
            return _scheduler;

        }
        /// <summary>
        /// 时间点cron表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cronExp"></param>
        /// <param name="jobName"></param>
        /// <param name="triggerName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>

        public static async Task<IScheduler> StartAsync<T>(string cronExp, string jobName, string triggerName, string groupName) where T : IJob
        {
            DateTimeOffset runTime = DateBuilder.EvenSecondDate(DateTime.Now);
            IJobDetail job = JobBuilder.Create<T>().WithIdentity(jobName, groupName).Build();
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(triggerName, groupName).WithCronSchedule(cronExp).StartAt(runTime).Build();
            if (_scheduler == null)
            {
                ISchedulerFactory factory = new StdSchedulerFactory();
                _scheduler = await factory.GetScheduler();
            }
            await _scheduler.ScheduleJob(job, trigger);
            await _scheduler.Start();
            return _scheduler;
        }

       



        public static async Task StopAllTask()
        {
            await _scheduler.PauseAll();
        }


        public static async Task<List<JobKey>> ShowAllTask()
        {
           
            GroupMatcher<JobKey> matcher = GroupMatcher<JobKey>.AnyGroup();
            var jobkeys = await _scheduler.GetJobKeys(matcher);
            return jobkeys.ToList();
        }

        public static async Task DeleteTask(JobKey jobkey)
        {
            await _scheduler.DeleteJob(jobkey);
        }




    }
}
